package com.ruoyi.SuveryInterface.NegativeSuvery;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.SuveryInterface.Imp.domain.SysNegative;
import com.ruoyi.SuveryInterface.Imp.domain.SysNegativeDetails;
import com.ruoyi.SuveryInterface.Imp.mapper.SysNegativeDetailsMapper;
import com.ruoyi.SuveryInterface.Imp.mapper.SysNegativeMapper;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class NegativeSuvery {
    //接⼝地址
    private static String apiUrl = "https://qyss.cjbdi.com/qyss/qyss/sfss/risk/score/";

    //加密密钥
    private static String key = "QCzpcXY1eidkQmcq";
    //接⼝调⽤凭证
    private static String ticket = "o0djGDRrMr9QN4xpknZ7C3wuNDcu3T+Ylz0XRByOl7k=";

    private SysNegativeMapper negativeMapper;

    private SysNegativeDetailsMapper negativeDetailsMapper;

    public NegativeSuvery(SysNegativeMapper negativeMapper, SysNegativeDetailsMapper negativeDetailsMapper) {
        this.negativeMapper = negativeMapper;
        this.negativeDetailsMapper = negativeDetailsMapper;
    }

    public SysNegativeMapper getNegativeMapper() {
        return negativeMapper;
    }

    public void setNegativeMapper(SysNegativeMapper negativeMapper) {
        this.negativeMapper = negativeMapper;
    }

    public SysNegativeDetailsMapper getNegativeDetailsMapper() {
        return negativeDetailsMapper;
    }

    public void setNegativeDetailsMapper(SysNegativeDetailsMapper negativeDetailsMapper) {
        this.negativeDetailsMapper = negativeDetailsMapper;
    }

    public SysNegative QueryNegative(String name, String id, Integer authorize, Long orderId) throws Exception {


        if (true) {
            throw new Exception();
        }


        RestTemplate restTemplate = new
                RestTemplate();
        HttpComponentsClientHttpRequestFactory requestFactory
                = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setConnectionRequestTimeout(60000);
        requestFactory.setConnectTimeout(60000);
        restTemplate.setRequestFactory(requestFactory);

//⼊参封装
        JSONArray array = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", name);
        jsonObject.put("id", id);
        jsonObject.put("authorize", authorize);
        array.add(jsonObject);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("ticket", ticket);

//需要对⼊参进⾏ aes 加密
            String requestBody = AES.aesEncrypt(array.toJSONString(), key);
//设置访问的 Entity
            HttpEntity entity = new HttpEntity<>(requestBody, headers);
//执⾏ post 请求
            ResponseEntity<String> result =
                    restTemplate.exchange(apiUrl, HttpMethod.POST, entity, String.class);
//接收到返回结果需要进⾏ aes 解密
            String code = AES.aesDecrypt(result.getBody(), key);


            SysNegative negative = getNegative(code);

            negative.setOrderId(orderId);
            //执行插入操作
            negativeMapper.insertSysNegative(negative);
            List<JSONObject> datas = negative.getData();
            List<SysNegativeDetails> negativeDetails = new ArrayList<>();
            for (JSONObject data : datas) {
                data.put("data_id",data.get("id"));
                data.remove("id");
                data.put("sys_negative_id",negative.getId());

                SysNegativeDetails sysNegativeDetails = data.toJavaObject(SysNegativeDetails.class);
                negativeDetails.add(sysNegativeDetails);
            }

            negativeDetailsMapper.insertBatch(negativeDetails);
            negative.setNegativeDetails(negativeDetails);


            return  negative;




    }


    private SysNegative getNegative(String code) {
        SysNegative sysNegative = JSONObject.parseObject(code, SysNegative.class);

        List<JSONObject> datas = JSONObject.parseObject(String.valueOf(sysNegative.getData()), List.class);

        sysNegative.setCreateTime(new Date());
        sysNegative.setCreateBy(1l);



        return sysNegative;
    }

}
